package com.hzjy.manage.controller;


import com.hzjy.api.beans.HzjyAgreement;
import com.hzjy.api.beans.HzjyUserPayRecord;
import com.hzjy.api.service.impl.HzjyAgreementServiceImpl;
import com.hzjy.api.vo.AgreementDTO;
import com.hzjy.common.core.controller.BaseController;
import com.hzjy.common.core.domain.AjaxResult;
import com.hzjy.common.core.page.TableDataInfo;
import com.hzjy.manage.service.impl.UserPayRecordServiceImpl;
import com.hzjy.api.vo.UserPayRecordDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 发票管理
 *
 * @author hzjy
 * @date 2020-08-23
 */
@RestController
@RequestMapping("/manage/user_pay_record")
public class UserPayRecordController extends BaseController {

    @Autowired
    private UserPayRecordServiceImpl userPayRecordService;
    @Autowired
    private HzjyAgreementServiceImpl hzjyAgreementService;

    /**
     * 订单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:lnvoice_management:list')")
    @PostMapping("/list")
    public AjaxResult list(@RequestBody HzjyUserPayRecord hzjyUserPayRecord){
        Integer currentPage = hzjyUserPayRecord.getCurrentPage();
        Integer size = hzjyUserPayRecord.getSize();
        if (currentPage != null && size != null){
            hzjyUserPayRecord.setCurrentPage((currentPage -1)*size);
        }else {
            hzjyUserPayRecord.setCurrentPage(0);
            hzjyUserPayRecord.setSize(10);
        }

        Map map = userPayRecordService.selectOrderNum(hzjyUserPayRecord);
        List<HzjyUserPayRecord> hzjyUserPayRecordList = userPayRecordService.list(hzjyUserPayRecord);
        //定义一个计算当前页返回数据，金额统计
        BigDecimal count = new BigDecimal("0.00");
        if(!CollectionUtils.isEmpty(hzjyUserPayRecordList)){
            for (HzjyUserPayRecord userPayRecord : hzjyUserPayRecordList) {
                if (userPayRecord.getStatus() == 1){
                    count = count.add(userPayRecord.getPayMoney());
                }
            }
        }
        map.put("payRecordList", hzjyUserPayRecordList);
        map.put("count",count);
        return AjaxResult.success(200,"查询成功",map);
    }

    /**
     * 发票列表
     */
    @PreAuthorize("@ss.hasPermi('manage:lnvoice_management:payRecordList')")
    @PostMapping("/payRecordList")
    public TableDataInfo payRecordList(@RequestBody HzjyUserPayRecord hzjyUserPayRecord){
        startPage();
        List<HzjyUserPayRecord> hzjyUserPayRecordList = userPayRecordService.payRecordList(hzjyUserPayRecord);
        return getDataTable(hzjyUserPayRecordList);
    }

    /**
     * 发票审核
     */
    @PreAuthorize("@ss.hasPermi('manage:lnvoice_management:agree')")
    @PostMapping("/agreePayRecord")
    public AjaxResult agreePayRecord(@RequestBody HzjyUserPayRecord hzjyUserPayRecord){
        userPayRecordService.agreePayRecord(hzjyUserPayRecord);
        return AjaxResult.success();
    }

    /**
     * 发票详情
     */
    @PreAuthorize("@ss.hasPermi('manage:lnvoice_management:one')")
    @PostMapping("/userPayRecordOne")
    public AjaxResult userPayRecordOne(@RequestBody HzjyUserPayRecord hzjyUserPayRecord){
        HzjyUserPayRecord userPayRecord = userPayRecordService.userPayRecordOne(hzjyUserPayRecord.getId());
        return AjaxResult.success(userPayRecord);
    }

    /**
     * 合同列表
     */
    @PreAuthorize("@ss.hasPermi('manage:lnvoice_management:agreementList')")
    @PostMapping("/agreementList")
    public AjaxResult agreementList(@RequestBody AgreementDTO agreementDTO){
        Integer currentPage = agreementDTO.getCurrentPage();
        Integer size = agreementDTO.getSize();
        if (currentPage != null && size != null){
            agreementDTO.setCurrentPage((currentPage -1)*size);
        }else {
            agreementDTO.setCurrentPage(0);
            agreementDTO.setSize(10);
        }
        List<AgreementDTO> agreementList = hzjyAgreementService.selectAgreementList(agreementDTO);
        Integer total = hzjyAgreementService.selectAgreementNum(agreementDTO);
        Map map = new HashMap<>();
        map.put("agreementList", agreementList);
        map.put("total",total);
        return AjaxResult.success(200,"查询成功",map);
    }
}